home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 1.iso
/
ARGONET
/
PD
/
MATHS
/
RLAB
/
RLAB125.ZIP
/
!RLaB
/
help_ai
/
if
< prev
next >
Wrap
Text File
|
1995-02-18
|
2KB
|
88 lines
if:
1) Introduction ------------------------------------------------
The if statement is very similar to the C if statement.
if ( test-expression )
{
statement(s)
else
statement(s)
}
The "else" is optional, and the braces are always required.
The differences between the C-language if-statement and RLaB's
are due to the special demands of an interactive language.
Examples:
//-----------------------------------
if ( init )
{
mass = 10.0;
inertia = 3*mass/length^3;
init = 0;
}
//-----------------------------------
if(class(data) == "string")
{
fprintf(file, data);
else
write(file, data);
}
//-----------------------------------
if( class(v) != "matrix" ) { error(); }
//-----------------------------------
At present there is no explicit elseif statement. However the
else if behavior can be implemented as it is in C (although
RLaB's syntax is clumsier). For example:
if( test1 )
{
x = a;
else if( test2 ) {
x = b;
else if( test3 ) {
x = c;
else
error();
}}}
2) Finer Points -----------------------------------------------
The if statement requires that the test-expression be a scalar
quantity. If it is necessary to test a matrix, the result of
the matrix test must be reduced to a scalar quantity. The
any(), all() and find() functions are useful in this
situation.
Example:
> a = [1,2,3;4,5,6;7,8,9];
> a < 100
1 1 1
1 1 1
1 1 1
> all (a < 100)
1 1 1
> all (all (a < 100))
1
In the above example, the result of a matrix test was first
reduced to a vector result with one use of all(). A second use
of all() reduces the vector result to a scalar result, which
is now suitable for inclusion in an if statement
test-expression.
> if (all (all (a < 100))) { "all a < 100" }
all a < 100